package com.sys.gradepaysys.service.impl;

import com.sys.gradepaysys.mapper.StudentMapper;
import com.sys.gradepaysys.model.Clazz;
import com.sys.gradepaysys.model.Grade;
import com.sys.gradepaysys.model.Student;
import com.sys.gradepaysys.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    StudentMapper studentMapper;
    @Override
    public Student login(String username, String password) {
        Student students = studentMapper.selectStuList(username);
//        Student student = new Student();
//        if (students.size() == 1) {
//            for (Student student1 : students) {
//                student = student1;
//            }
        if (students != null) {
            
            if (!students.getPassword().equals(password)) {
                throw new RuntimeException("密码不正确!");
            }
        } else {
            throw new RuntimeException("该学号不存在!");
        }
        return students;
    }

    @Override
    public List<Grade> getAllGrade() {
        List<Grade> allGrade = studentMapper.selectGradeList();
        return allGrade;
    }

    @Override
    public List<Clazz> getAllClazz(String gid) {
        return studentMapper.selectClazzList(gid);
    }

    @Override
    public boolean registerStu(Student student) {
        Integer saveResult = studentMapper.save(student);
        if (saveResult != 1) {
            return false;
        }
        return true;
    }

    @Override
    public List<Map<String, String>> getStuOneList(String sid) {
        List<Map<String, String>> stuOneList = studentMapper.getStuOneList(sid);
        return stuOneList;
    }

    @Override
    public boolean updatePwd(String password, String sid) {
        Integer integer = studentMapper.updatePwd(password, sid);
        if (integer != 1) {
            return false;
        }
        return true;
    }

    @Override
    public List<Clazz> selectClazzByGid(String cid, String grade, String clazz) {
       return studentMapper.selectClazzByGid(cid, grade, clazz);
    }

    @Override
    public Student getUserByName(String username) {
        Student students = studentMapper.selectStuList(username);
        return students;
    }
}
